package com.ciwong.mobilelib.utils;

import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.zip.ZipUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by res-mingyang on 2015/7/30.
 */
public class ZipHelper
{
    private static ZipHelper instance;

    private static List<String> mList;

    private ZipHelper()
    {

    }

    public static ZipHelper getInstance()
    {
        if (instance == null)
        {
            instance = new ZipHelper();
            mList = new ArrayList<String>();
        }
        return instance;
    }

    public void addToUpZipTask(final String srcPath, final String dstFolderPath,
            final BaseCallBack callBack)
    {
        ThreadTask.getInstance().executorOtherThread(new Runnable()
        {
            @Override
            public void run()
            {
                synchronized (mList)
                {
                    File file = new File(srcPath);
                    try {
                        mList.add(srcPath);
                        ZipUtils.upFile(file, dstFolderPath);
                        if (callBack != null) {
                            callBack.success(srcPath);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (callBack != null) {
                            callBack.failed("解压缩失败");
                        }
                    } catch (OutOfMemoryError error) {
                        
                    } finally {
                        mList.remove(srcPath);
                        if (file.exists()) {
                            file.delete();
                        }
                    }
                }
            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    public boolean addToUpZipTask(final String srcPath,
            final String dstFolderPath)
    {
        synchronized (mList)
        {
            boolean flag = true;
            File file = new File(srcPath);
            try {
                mList.add(srcPath);
                if (ZipUtils.upFile(file, dstFolderPath)) {
                    mList.remove(srcPath);
                }
            } catch (OutOfMemoryError error) {

            } catch (Exception e) {
                e.printStackTrace();
                flag = false;
            } finally {
                if (file.exists()) {
                    file.delete();
                }
            }
            return flag;
        }
    }

    public boolean isUpZiping(String srcPath)
    {
        synchronized (mList)
        {
            if (mList.contains(srcPath))
            {
                return true;
            }
            return false;
        }
    }

    public void addToZipTask(final List<String> srcPaths, final String zipFile,
            final BaseCallBack callBack)
    {
        ThreadTask.getInstance().executorOtherThread(new Runnable()
        {
            @Override
            public void run()
            {
                if (addToZipTask(srcPaths, zipFile))
                {
                    if (callBack != null)
                    {
                        callBack.success(zipFile);
                    }
                }
                else
                {
                    if (callBack != null)
                    {
                        callBack.failed("解压缩失败");
                    }
                }
            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    /**
     * 打包答案
     * 
     * @param srcPaths
     *            打包源文件
     * @param zipFile
     *            保存地址
     * @return
     */
    public boolean addToZipTask(final List<String> srcPaths,
            final String zipFile)
    {
        List<File> list = new ArrayList<File>();
        if (srcPaths == null || srcPaths.size() == 0)
        {
            return false;
        }
        for (String path : srcPaths)
        {
            ZipFile file = new ZipFile(path);
            if (path == null || list.contains(file))
            {
                continue;
            }
            list.add(file);
        }
        try
        {
            ZipUtils.zipFiles(list, new File(zipFile));
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private class ZipFile extends File
    {
        private String path;

        public ZipFile(String path)
        {
            super(path);
            this.path = path;
        }

        @Override
        public boolean equals(Object obj)
        {
            if (path == null)
            {
                return false;
            }
            return path.equals(((ZipFile) obj).getPath());
        }
    }
}
